package com.atguigu.jinx.mapper;

import com.atguigu.jinx.entity.dto.JobTask;
import com.atguigu.jinx.entity.vo.JobDetailVO;
import com.atguigu.jinx.entity.vo.PortalJobListVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author steve
 * @since 2025-08-07
 */
@Mapper
public interface JobTaskMapper extends BaseMapper<JobTask> {


    @Select("SELECT \n" +
            "  jt.id AS jobId,\n" +
            "  jt.title,\n" +
            "  jt.city,\n" +
            "  jt.job_type,\n" +
            "  MAX(CASE WHEN jtsr.days = 7 THEN jtsr.days END) AS shortTermSettlementWorkDay,\n" +
            "  MAX(CASE WHEN jtsr.days = 7 THEN jtsr.amount END) AS shortTermSettlementAmount,\n" +
            "  MAX(CASE WHEN jtsr.days = 15 THEN jtsr.days END) AS longTermSettlementWorkDay,\n" +
            "  MAX(CASE WHEN jtsr.days = 15 THEN jtsr.amount END) AS longTermSettlementAmount\n" +
            "FROM job_task jt\n" +
            "LEFT JOIN job_task_settlement_rule jtsr ON jt.id = jtsr.job_task_id\n" +
            "WHERE jt.is_deleted = 0\n" +
            "GROUP BY jt.id, jt.title, jt.city, jt.job_type\n" +
            "ORDER BY jt.salary DESC LIMIT 9;\n")
    List<PortalJobListVO> selectPortalJobs();

    @Select({
            "<script>",
            "SELECT ",
            "  jt.*, ",
            "  MAX(CASE WHEN jtsr.days = 7 THEN jtsr.days END) AS shortTermSettlementWorkDay, ",
            "  MAX(CASE WHEN jtsr.days = 7 THEN jtsr.percent END) AS shortTermPercent, ",
            "  MAX(CASE WHEN jtsr.days = 7 THEN jtsr.amount END) AS shortTermSettlementAmount, ",
            "  MAX(CASE WHEN jtsr.days = 15 THEN jtsr.days END) AS longTermSettlementWorkDay, ",
            "  MAX(CASE WHEN jtsr.days = 15 THEN jtsr.amount END) AS longTermSettlementAmount, ",
            "  MAX(CASE WHEN jtsr.days = 15 THEN jtsr.percent END) AS longTermPercent ",
            "FROM job_task jt ",
            "LEFT JOIN job_task_settlement_rule jtsr ON jt.id = jtsr.job_task_id ",
            "WHERE jt.is_deleted = 0 ",
            "AND jt.id = #{jobId} ",
            "<choose>",
            "   <when test='userId != null and userId != \"\"'>",
            "       AND jt.status = '1' ",
            "       AND jt.user_id = #{userId} ",
            "   </when>",
/*            "   <otherwise>",
            "       AND jt.status = '0' ",
            "   </otherwise>",*/
            "</choose>",
            "GROUP BY jt.id, jt.title, jt.city, jt.job_type, jt.salary ",
            "ORDER BY jt.salary DESC",
            "</script>"
    })
    JobDetailVO selectDetails(@Param("jobId") String jobId, @Param("userId") String userId);


    @Select("SELECT DISTINCT job_type FROM `job_task`WHERE title is not null ")
    List<String> getDistinctTitles();

    @Select("SELECT DISTINCT city FROM `job_task` WHERE city is not null ")
    List<String> getDistinctCities();
}
